package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LDictType;
import com.learn.core.domain.LOrganization;
import com.learn.core.mapper.LDictTypeMapper;
import com.learn.core.service.ILDictTypeService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:39:07
 * @describe 字典类型表服务实现类
 */
@Service
public class LDictTypeServiceImpl extends BaseServiceImpl<LDictTypeMapper, LDictType> implements ILDictTypeService {

    @Autowired
    LDictTypeMapper lDictTypeMapper;

    @Override
    public List<LDictType> queryLDictTypeAll() {
        List<LDictType> treeMenus =new ArrayList<>();
        List<LDictType> lDictTypeList = lDictTypeMapper.selectList(null);
        // 查询树结构
        for(LDictType lDictType : getDictTypeNode(lDictTypeList)) {
            lDictType=buildChilTree(lDictType,lDictTypeList);
            treeMenus.add(lDictType);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LDictType buildChilTree(LDictType pNode,List<LDictType> dictTypeList){
        List<LDictType> chilDictType =new ArrayList<>();
        for(LDictType menuNode : dictTypeList) {
            if(menuNode.getParentNode() == pNode.getDictTypeId()) {
                chilDictType.add(buildChilTree(menuNode,dictTypeList));
            }
        }
        pNode.setChilDictType(chilDictType);
        return pNode;
    }

    //获取根节点
    private List<LDictType> getDictTypeNode(List<LDictType> lDictTypes) {
        List<LDictType> lDictTypeList =new  ArrayList<LDictType>();
        for(LDictType lDictType : lDictTypes) {
            if(lDictType.getParentNode() == 0) {
                lDictTypeList.add(lDictType);
            }
        }
        return lDictTypeList;
    }
}
